package com.lyn.pluginhost.activity

import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.lyn.pluginapi.api.PluginApi
import com.lyn.pluginapi.factory.ClassLoaderFactory
import com.lyn.pluginapi.factory.ResourcesFactory
import com.lyn.pluginapi.jump.JumpHelper

/**
 * 代理插件activity
 * @author longyn 2025/07/04
 * @version 1.0.0
 */
class ProxyPluginActivity : AppCompatActivity() {

    private var pluginApi: PluginApi?=null
    private lateinit var intentActivityResultLauncher:ActivityResultLauncher<Intent>
    lateinit var pluginId:String
    lateinit var pluginClassName:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        try {
            pluginId = JumpHelper.instance.springboardPluginId
            pluginClassName = JumpHelper.instance.springboardPluginClassName
            val loadClass = classLoader.loadClass(JumpHelper.instance.springboardPluginClassName)
            pluginApi = loadClass.getDeclaredConstructor().newInstance() as PluginApi
            pluginApi?.onProxyActivity(JumpHelper.instance.springboardPluginId,this)
            intentActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
                pluginApi?.onActivityResultCallback(it)
            }
            onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(
                pluginApi?.isHandleOnBackPressedEnabled() == true){
                override fun handleOnBackPressed() {
                    pluginApi?.onHandleOnBackPressed()
                }
            })
            pluginApi?.onCreate(savedInstanceState)
            ProxyPluginActivityStack.instance.push(this)
        } catch (e: Throwable) {
            e.printStackTrace()
        }
    }

    override fun onStart() {
        super.onStart()
        pluginApi?.onStart()
    }

    override fun onStop() {
        super.onStop()
        pluginApi?.onStart()
    }

    override fun onResume() {
        super.onResume()
        pluginApi?.onResume()
    }

    override fun onPause() {
        super.onPause()
        pluginApi?.onPause()
        if (isFinishing){
            intentActivityResultLauncher.unregister()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        pluginApi?.onDestroy()
        ProxyPluginActivityStack.instance.remove(this)
    }

    override fun getResources(): Resources {
        ResourcesFactory.get(JumpHelper.instance.springboardPluginId)?.let { resources->
            return resources
        }
        return super.getResources()
    }

    override fun getClassLoader(): ClassLoader {
        ClassLoaderFactory.get(JumpHelper.instance.springboardPluginId)?.let { classLoader->
            return classLoader
        }
        return super.getClassLoader()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        pluginApi?.let {
            if (it.dispatchTouchEvent(ev)){
                return true
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        pluginApi?.let {
            if (it.dispatchKeyEvent(event)){
                return true
            }
        }
        return super.dispatchKeyEvent(event)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        pluginApi?.let {
            if (it.onKeyDown(keyCode, event)){
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean {
        pluginApi?.let {
            if (it.onKeyLongPress(keyCode, event)){
                return true
            }
        }
        return super.onKeyLongPress(keyCode, event)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        pluginApi?.let {
            if (it.onKeyUp(keyCode, event)){
                return true
            }
        }
        return super.onKeyUp(keyCode, event)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        pluginApi?.let {
            if (it.onOptionsItemSelected(item)){
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        pluginApi?.let {
            if (it.onCreateOptionsMenu(menu)){
                return true
            }
        }
        return super.onCreateOptionsMenu(menu)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        pluginApi?.onNewIntent(intent)
    }

}